//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Globals.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TKanaForm *KanaForm;
//---------------------------------------------------------------------------
__fastcall TKanaForm::TKanaForm(TComponent* Owner)
 : TForm(Owner)
{
}


//--------------------------------//
//                                //
//--------------------------------//
void __fastcall TKanaForm::ShowText( BYTE *String, int  flag )
{
  WORD val[4];
  BYTE *TextBuf = String;
  char buf[256];
  bool cwFirst;
  static AnsiString buffer;

  if ( flag != 0x10 )
  { //
    if ( flag ) kanaEdit->Clear( );
    buffer = "";
    //
//    if ( flag || txt.ptr_index == last_ptr_index ) return;
    if ( flag ) return;
    // We displayed it so display no more
    last_ptr_index = txt.ptr_index;
    kanaEdit->Clear( );
    //------------------------//
    //                        //
    //------------------------//
    kanaEdit->Lines->BeginUpdate( );
  }

  txt.data = 0;
  while( txt.data != 0xFF )
  { //
    txt.data = TextBuf[0];
    TextBuf += 1;

    //----------------//
    // End Text Write //
    //----------------//
    if ( txt.data == 0xFF )
    {
      if ( flag == 0x10 ) return;
      break;
    }

    //-------------//
    // Compression //
    //-------------//
    if ( txt.data >= 0xF0 && txt.data <= 0xF3 )
    { //
      txt.data    = TextBuf[0] + ( (txt.data & 0x03) << 8 );
      TextBuf    += 1;
      //
      txt.data    = WGET( rom.data, Snes2Pc( 0x8FF900 + ( txt.data << 1)) );
//      TextBuf     = &rom[ Snes2Pc( 0x8F0000 + txt.data ) ];
      //
      ShowText( &rom.data[ Snes2Pc( 0x8F0000 + txt.data ) ], 0x10 );
      txt.data    = 0;
      continue;
    }

    //------------------//
    // Item from memory //
    //------------------//
    if ( txt.data == 0xF4 ){
      txt.data     = TextBuf[0];
      TextBuf += 1;
      sprintf( buf, "[Name$%02X]", txt.data );
      buffer = buffer + AnsiString( buf );
      continue;
    }

    //----------------//
    // Unknowm        //
    //----------------//
    if ( txt.data == 0xFA ) {
      txt.data     = TextBuf[0] + (txt.data << 8);
      TextBuf += 1;
      sprintf( buf, "[$%04X]", txt.data );
      buffer = buffer + AnsiString( buf );
      continue;
    }

    //-------------//
    // Text Color  //
    //-------------//
    if ( txt.data == 0xF7 ){
      txt.data     = TextBuf[0];
      TextBuf += 1;
      sprintf( buf, "[Color$%02X]", txt.data );
      buffer = buffer + AnsiString( buf );
      continue;
    }

    //-------------//
    // Text Speed  //
    //-------------//
    if ( txt.data == 0xF8 ){
      txt.data     = TextBuf[0];
      TextBuf += 1;
      sprintf( buf, "[Pause$%02X]", txt.data );
      buffer       = buffer + AnsiString( buf );
      continue;
    }

    //-------------//
    // Line Break; //
    //-------------//
    if ( txt.data == 0xFE ){
      buffer       = buffer + "\r\n";
      continue;
    }

    //----------------------------//
    //                            //
    //----------------------------//
    if ( duals[ ((txt.data & 0xFF) << 8) + TextBuf[0] ].length )
    {
      buffer = buffer + AnsiString( (char *)duals[ ((txt.data & 0xFF) << 8) + TextBuf[0] ].text );
      TextBuf += 1;      
    }
    else
    if ( codes[ txt.data & 0xFF ].length )
      buffer = buffer + AnsiString( (char *)codes[ txt.data & 0xFF ].text );
    else
      buffer = buffer + AnsiString( "[$" + IntToHex(txt.data, 2 ) + "]" );
  } // WEND

  txt.data       = 0;
  kanaEdit->Text = buffer;
  kanaEdit->Lines->EndUpdate( );
}

//--------------------------------//
//                                //
//--------------------------------//
void __fastcall TKanaForm::FormCreate(TObject *Sender)
{
  last_ptr_index = -1;
}

